package com.sy.simpleegg.ui.horizontalrecyclerview

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import java.lang.ref.WeakReference

class HorizontalRecyclerView : RecyclerView {
    private var delaySmooth: Long = 5000
    private var targetPosition = 0
    private var delaySmoothTask: DelaySmoothTask
    private val mLastTouchX = 0
    private val mLastTouchY = 0

    constructor(context: Context) : super(context) {
        delaySmoothTask = DelaySmoothTask(this)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        delaySmoothTask = DelaySmoothTask(this)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        delaySmoothTask = DelaySmoothTask(this)
    }

    override fun onInterceptTouchEvent(e: MotionEvent): Boolean {

//        Log.d("onInterceptTouchEvent", "MotionEvent:" + e.getAction());
//        final int action = e.getActionMasked();
//
//        if (action == MotionEvent.ACTION_DOWN){
//            mLastTouchX = (int) (e.getX() + 0.5f);
//            mLastTouchY = (int) (e.getY() + 0.5f);
//        }else if (action == MotionEvent.ACTION_MOVE){
//            final int x = (int) (e.getX() + 0.5f);
//            final int y = (int) (e.getY() + 0.5f);
//            int dx = x - mLastTouchX;
//            int dy = y - mLastTouchY;
//            Log.d("HorizontalRecyclerView", "move_x:" + dx + " move_y:" + dy);
//            if (Math.abs(dx) - Math.abs(dy) > 0){
//                Log.d("HorizontalRecyclerView", "MotionEvent:Math.abs(dx) - Math.abs(dy) > 0");
//                return true;
//            }
//        }else if (action == MotionEvent.ACTION_CANCEL){
//            final int x = (int) (e.getX() + 0.5f);
//            final int y = (int) (e.getY() + 0.5f);
//            int dx = x - mLastTouchX;
//            int dy = y - mLastTouchY;
//            Log.d("HorizontalRecyclerView", "cancel_x:" + dx + " cancel_y:" + dy);
//            if (Math.abs(dx) - Math.abs(dy) > 0){
//                Log.d("HorizontalRecyclerView", "MotionEvent:Math.abs(dx) - Math.abs(dy) > 0");
//                return true;
//            }
//        }
        if (e.action == MotionEvent.ACTION_DOWN && this.scrollState == SCROLL_STATE_SETTLING) {
            stopScroll()
        }
        return super.onInterceptTouchEvent(e)
        //        return false;
    }

    override fun onTouchEvent(e: MotionEvent): Boolean {
//        final int action = e.getActionMasked();
//        switch (action){
//            case MotionEvent.ACTION_DOWN:
//                Log.d("HorizontalRecyclerView", "action_down");
//                mLastTouchX = (int) (e.getX() + 0.5f);
//                mLastTouchY = (int) (e.getY() + 0.5f);
//                break;
//            case MotionEvent.ACTION_MOVE:
//                Log.d("HorizontalRecyclerView", "action_move");
//                final int x = (int) (e.getX() + 0.5f);
//                final int y = (int) (e.getY() + 0.5f);
//                int dx = x - mLastTouchX;
//                int dy = y - mLastTouchY;
//                Log.d("HorizontalRecyclerView", "move_x:" + dx + " move_y:" + dy);
//                if (Math.abs(dx) - Math.abs(dy) < 0){
//                    Log.d("HorizontalRecyclerView", "MotionEvent:Math.abs(dx) - Math.abs(dy) > 0");
//                    return true;
//                }
//                break;
//            case MotionEvent.ACTION_UP:
//                Log.d("HorizontalRecyclerView", "action_up");
//                break;
//            case MotionEvent.ACTION_CANCEL:
//                Log.d("HorizontalRecyclerView", "action_cancel");
//                break;
//        }
        return super.onTouchEvent(e)
    }

    fun delaySmoothScrollToPosition(millisecond: Long, position: Int) {
        delaySmooth = millisecond
        targetPosition = position
        removeCallbacks(delaySmoothTask)
        postDelayed(delaySmoothTask, delaySmooth)
    }

    inner class DelaySmoothTask(recyclerView: HorizontalRecyclerView) : Runnable {
        var mReference: WeakReference<HorizontalRecyclerView>

        init {
            mReference = WeakReference(recyclerView)
        }

        override fun run() {
            val horizontalRecyclerView = mReference.get()
            horizontalRecyclerView!!.smoothScrollToPosition(targetPosition)
        }
    }
}
